package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.UserBean;
import com.wantong.jianpu.models.OtherModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.XUtils;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

/**
 * @author PACY
 * @date 2022/4/1
 * description:
 */
public class MainViewModel extends BaseViewModel {
    public OtherModel model;
    public SingleLiveEvent<Integer> msgCount=new SingleLiveEvent<>();
    public MainViewModel(@NonNull Application application) {
        super(application);
        model=new OtherModel();
    }

    @Override
    protected void init() {
        super.init();
        getUserInfo();
        getMessage();
    }

    /**
     * 获取个人信息
     */
    public void getUserInfo(){
        model.getUserInfo(new BaseObserver<UserBean>() {
            @Override
            public void onSuccess(BaseResultBean<UserBean> t) {
                XUtils.saveUser(t.getData());
            }
        },getLifecycleProvider());
    }

    /**
     * 获取未读消息条数
     */
    public void getMessage(){
        model.getMessage(new BaseObserver<Integer>() {
            @Override
            public void onSuccess(BaseResultBean<Integer> t) {
                if(t.getData()==null){
                    msgCount.setValue(0);
                }else{
                    msgCount.setValue(t.getData());
                }
            }

        },getLifecycleProvider());
    }
}
